package com.book.items.composite;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;
import lombok.experimental.Accessors;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
@Data
@NoArgsConstructor
@AllArgsConstructor
@Accessors(chain = true)
public class ProductComposite extends AbstractProductItem {

    private int id;
    private int pid;

    private String name;

    /**
     * 这里完全没有必要使用父级抽象，应为扩展可能性极小
     */
    private List<AbstractProductItem> child = new ArrayList<>();


    @Override
    protected void addProductItem(AbstractProductItem item) {
        this.child.add(item);
    }

    @Override
    protected void delProductItem(AbstractProductItem item) {
        ProductComposite removeItem = (ProductComposite) item;
        Iterator<AbstractProductItem> iterator = child.iterator();
        while (iterator.hasNext()) {
            ProductComposite next = (ProductComposite)iterator.next();
            if (removeItem.getId() == next.getId()) {
                iterator.remove();;
                break;
            }
        }
    }
}
